﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.DomainServices.Server;
using System.Text;
using Arch.DomainServices.Metadata;
using Arch.Samples.Model;

namespace Arch.Samples.RiaServices.Services
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class RiaMetadataProviderAttribute : DomainMetadataProviderAttribute
    {
        public override void Configure(MetadataBuilder builder)
        {
            builder
                .ForEntity<Todo>()
                    .ForProperty(e => e.CreationUser,
                        new IncludeAttribute(),
                        new AssociationAttribute("Todo_User", "CreationUserId", "Id") { IsForeignKey = true })
                    .ForProperty(e => e.GlobalCategory,
                        new IncludeAttribute(),
                        new AssociationAttribute("Todo_GlobalCategory", "GlobalCategoryId", "Id") { IsForeignKey = true })
                    .ForProperty(e => e.SubCategory,
                        new IncludeAttribute(),
                        new AssociationAttribute("Todo_SubCategory", "SubCategoryId", "Id") { IsForeignKey = true })

                .ForEntity<User>()
                    .ForProperty(e => e.Todoes,
                        new AssociationAttribute("Todo_User", "Id", "CreationUserId"))

                .ForEntity<GlobalCategory>()
                    .ForProperty(e => e.SubCategories, new AssociationAttribute("SubCategory_GlobalCategory", "Id", "GlobalCategoryId"))
                    .ForProperty(e => e.Todoes, new AssociationAttribute("Todo_GlobalCategory", "Id", "GlobalCategoryId"))

                .ForEntity<SubCategory>()
                    .ForProperty(e => e.GlobalCategory,
                        new IncludeAttribute(),
                        new AssociationAttribute("SubCategory_GlobalCategory", "GlobalCategoryId", "Id") { IsForeignKey = true })
                    .ForProperty(e => e.Todoes, new AssociationAttribute("Todo_SubCategory", "Id", "SubCategoryId"))
            ;
        }
    }
}
